<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './resize'
import { deviceList } from '@/api/iot/deviceLog';
export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '240px'
        },
        autoResize: {
            type: Boolean,
            default: true
        },
        chartData: {
            type: Object,
            // required: true,
        },
        num: {
            type: String || Number,
        },
        isNumber: {
            type: String || Number,

        },
        deviceType: {
            type: String || Number,

        },
        valueTimes: {
            type: Array,
            default: []
        }
    },
    data() {
        return {
            chart: null,
            isListData: [],
            timeData: [],
        }
    },
    watch: {
        num: {
            deep: true,
            handler(val) {
                this.listHistoryFun()
            }
        },
        isNumber: {
            deep: true,
            handler(val) {
                this.listHistoryFun()
            }
        },
        deviceType: {
            deep: true,
            handler(val) {
                this.listHistoryFun()
            }
        },
        valueTimes: {
            deep: true,
            handler(val) {
                if (val) {
                    this.timeData = val
                } else {
                    this.timeData = []
                }
                this.listHistoryFun()
            }
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        formatDate(timestamp) {
            const date = new Date(timestamp);
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            const hours = String(date.getHours()).padStart(2, '0');
            const minutes = String(date.getMinutes()).padStart(2, '0');
            const seconds = String(date.getSeconds()).padStart(2, '0');

            return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        },
        listHistoryFun() {

            if (!this.isNumber) {
                return
            }
            let startTime, endTime;
            const now = new Date();
            endTime = now.getTime();
            switch (this.num) {
                case '1':
                    // 半小时
                    startTime = now.getTime() - 30 * 60 * 1000;
                    break;
                case '2':
                    // 2小时
                    startTime = now.getTime() - 2 * 60 * 60 * 1000;
                    break;
                case '3':
                    // 24小时
                    startTime = now.getTime() - 24 * 60 * 60 * 1000;
                    break;
                default:
                    break;
            }
            let tmieS = this.formatDate(startTime)
            let tmieE = this.formatDate(endTime)

            if (this.timeData.length > 0) {
                tmieS = this.timeData[0]
                tmieE = this.timeData[1]
            }

            let _this = this
            deviceList({
                serialNumber: this.isNumber,
                // serialNumber: 'D13NT1TM0369',
                identify: this.deviceType,
                beginTime: tmieS,
                endTime: tmieE,
                pageNum: 1,
                pageSize: 9999999
            }).then((res) => {
                this.isListData = res.rows
                setTimeout(() => {
                    _this.setOptions()
                }, 2000)
            })
        },
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            this.listHistoryFun()
        },
        setOptions() {
            this.chart.setOption({
                animationDurationUpdate: 3000,
                xAxis: {
                    type: 'category',
                    axisLabel: {
                        color: '#409EFF', // x 轴标签文字颜色
                        formatter: function (value) {
                            // 假设每 4 个字符换行
                            return value.replace(/(.{10})/g, '$1\n');
                        }
                    },
                    data: this.isListData.map(item => item.createTime),
                },
                grid: {
                    left: 10,
                    right: 10,
                    bottom: 20,
                    top: 30,
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross'
                    },
                    padding: [5, 10]
                },
                yAxis: {
                    type: 'value',
                    minInterval: 1,
                },
                series: [
                    {
                        name: '设备状态',
                        type: 'line',
                        symbol: 'none',
                        sampling: 'lttb',
                        itemStyle: {
                            color: '#409EFF',
                        },
                        areaStyle: {},
                        data: this.isListData.map(item => item.funValue),
                    }]
            })
        }
    }
}
</script>
